package com.lc.luzhengtang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.lc.luzhengtang.R;
import com.lc.luzhengtang.db.BaseTable;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by Administrator on 2016/10/19.
 */
public class MessageAdapter extends ArrayAdapter<BaseTable.BaseA>{

    private LayoutInflater layoutInflater;
    private Context context;
    private int TYPE_ITEM = 0;
    private MessageAdapter.CheckInterface checkInterface;
    private NewsListTestAdapter.ModifyCountInterface modifyCountInterface;
    public MessageAdapter(Context context,int TYPE_ITEM, List<BaseTable.BaseA> lists) {
        super(context, 0, lists);

        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.TYPE_ITEM=TYPE_ITEM;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder ;

        final BaseTable.BaseA str = getItem(position);

        if (convertView == null) {

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_message_list, null)));

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (str.status == 1) {
            viewHolder.text_flag.setText("未读");
            viewHolder.text_flag.setTextColor(context.getResources().getColor(R.color.red));
            viewHolder.view_back_news.setBackgroundResource(R.drawable.background_circle);
        } else {
            viewHolder.text_flag.setText("已读");
            viewHolder.text_flag.setTextColor(context.getResources().getColor(R.color.gray2));
            viewHolder.view_back_news.setBackgroundResource(R.drawable.background_circle_gray);
        }
        viewHolder.item_message_title_tv.setText(str.message);
        viewHolder.item_message_time_tv.setText(str.time);
        //单选框按钮
        viewHolder.checkbox_news.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        str.setChoosed(((CheckBox) v).isChecked());
                        checkInterface.checkGroup(position, ((CheckBox) v).isChecked());//向外暴露接口
                    }
                }
        );
        return convertView;
    }



    class ViewHolder {

        @BoundView(R.id.item_message_title_tv) private TextView item_message_title_tv;
        @BoundView(R.id.item_message_time_tv) private TextView item_message_time_tv;
        @BoundView(R.id.text_flag) private TextView          text_flag;
        @BoundView(R.id.view_back_news) View view_back_news;

        @BoundView(R.id.checkbox_news) CheckBox checkbox_news;
    }
    /**
     * 复选框接口
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param position  元素位置
         * @param isChecked 元素选中与否
         */
        void checkGroup(int position, boolean isChecked);
    }
    /**
     * 改变商品数量接口
     *
     * @param modifyCountInterface
     */
    public void setModifyCountInterface(NewsListTestAdapter.ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }
    /**
     * 单选接口
     *
     * @param checkInterface
     */
    public void setCheckInterface(MessageAdapter.CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }



    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param position      组元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doIncrease(int position, View showCountView, boolean isChecked);

        /**
         * 删减操作
         *
         * @param position      组元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doDecrease(int position, View showCountView, boolean isChecked);

        /**
         * 删除子item
         *
         * @param position
         */
        void childDelete(int position);
    }
}
